﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SAP.WebServices;
using System.Data;
using ReadXMLFile;
using System.Collections;
using System.Globalization;
using Microsoft.ReportingServices.Rendering;
using Microsoft.Reporting.WebForms;
using System.Drawing.Printing;
using System.Management;
namespace SAP
{
    public partial class Popup_RequisitionPurchase : System.Web.UI.Page
    {

        /*
         * 
     <?xml version="1.0" encoding="utf-8"?>
<Report xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns:cl="http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition">
  
         */
        private LoginSession CurrentLoginSession;
        public static DataTable dtContents;
        public static DataTable dtHeader;
        private GeneralFunctions GF;
        int RequestID = -1;


        protected void Page_Load(object sender, EventArgs e)
        {
            SessionCheck();
            if (!IsPostBack)
            {

                foreach (string Printers in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
                {
                    String pt = Printers;
                }
                GetDefault df = new GetDefault();

                this.RequestID = Request.QueryString["requestID"] == null ? -1 : Request.QueryString["requestID"] == "" ? -1 : Int32.Parse(Request.QueryString["requestID"]);
                DataTable dtHeader = GF.ObjectSelect("usp_OffHeader_Select", "@hdrID", RequestID.ToString());
                DataTable dtContents = GF.ObjectSelect("usp_OffDetail_Select", "@hdrID", RequestID.ToString());

                if (dtHeader.Rows.Count < 1)
                    return;

                lb_PRNo.Text = RequestID.ToString();
                lb_Company.Text = CurrentLoginSession.CompanyName.ToString();
                lb_Depart.Text = "";
                lb_Store.Text = dtHeader.Rows[0]["DefaultWhsName"].ToString(); ;
                lb_Status.Text = getStatusName(Int32.Parse(dtHeader.Rows[0]["Status"].ToString()));

                string docDate = dtHeader.Rows[0]["DocDate"].ToString();
                string reqDate = dtHeader.Rows[0]["reqDate"].ToString();
                lb_PostingDate.Text = DateTime.Parse(docDate).ToString("dd/MM/yyyy");//docDate.Substring(6, 2) + "/" + docDate.Substring(4, 2) + "/" + docDate.Substring(0, 4);
                lb_RequestDate.Text = DateTime.Parse(reqDate).ToString("dd/MM/yyyy");
                               
                loadDetail(dtContents);
                
            }

        }

        private void loadDetail(DataTable dt) { 
            DataTable newDt = new DataTable();
            newDt.Columns.Add("No");
            newDt.Columns.Add("ItemCode");
            newDt.Columns.Add("Dscription");
            newDt.Columns.Add("invntryuom");
            newDt.Columns.Add("Quantity");
            newDt.Columns.Add("WhsCode");
            newDt.Columns.Add("CardName");
            newDt.Columns.Add("PriceAfVAT");
            GetDefault df = new GetDefault();
            DataTable tmp = new DataTable();
            foreach (DataRow dr in dt.Rows) {
                DataRow newR = newDt.NewRow();
                newR["No"] = dr["No"];
                newR["ItemCode"] = dr["ItemCode"];
                newR["Dscription"] = dr["Dscription"];
                newR["invntryuom"] = dr["invntryuom"];
                newR["Quantity"] = dr["Quantity"];
                newR["WhsCode"] = dr["WhsCode"];

                tmp = df.GetLastVendorPrice_PR(User.Identity.Name, dr["ItemCode"].ToString()).Tables[0];
                if (tmp.Rows.Count > 0)
                {
                    newR["CardName"] = tmp.Rows[0]["CardName"].ToString();
                    newR["PriceAfVAT"] = String.Format("{0:N}", tmp.Rows[0]["PriceAfVAT"]);
                }
                else {
                    newR["CardName"] = "";
                    newR["PriceAfVAT"] = "";
                }
                newDt.Rows.Add(newR);
            }
            lvContents.DataSource = newDt;
            lvContents.DataBind();
        }
        private string getStatusName(int status) {
            switch (status)
            {
                case 0:
                    return "Rejected";                   
                case 1:
                    return "Approved";
                case 2:
                    return "Pending";
                case 3:
                    return "Draft";
                default:
                    return "";
            }
        }
        protected void btn_Print_Click(object sender, ImageClickEventArgs e)
        {
           
            //Microsoft.Reporting.WebFormsClient.ReportViewer.invokePrintDialog
            //Microsoft.Reporting.WebForms.ReportViewer.in
            /*
            ReportPrintDocument rp = new ReportPrintDocument(ReportViewer1.LocalReport);

            System.Windows.Forms.PrintDialog pd = new System.Windows.Forms.PrintDialog();
            if (pd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                rp.PrinterSettings.PrinterName = pd.PrinterSettings.PrinterName; //"CutePDF Writer";
                rp.Print();
            }
            pd.Dispose();
             * */
            //rp.PrintController=
             
        }
        public void SessionCheck()
        {

            if (Session["LoginSession"] != null)
            {
                this.CurrentLoginSession = (LoginSession)Session["LoginSession"];
                GF = new GeneralFunctions(CurrentLoginSession.UserName);
            }
            else
            {
                Response.Redirect("~/SessionErrorPage.aspx");
            }

        }    
     
    }
}